#
#  Author: Hari Sekhon
#  Date: 2016-01-16 12:21:15 +0000 (Sat, 16 Jan 2016)
#
#  vim:ts=4:sts=4:sw=4:noet
#
#  https://github.com/harisekhon/Dockerfiles
#
#  If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help improve or steer this or other code I publish
#
#  https://www.linkedin.com/in/harisekhon
#

ifneq ("$(wildcard bash-tools)", "")
	BASH_TOOLS := bash-tools
else ifneq ("$(wildcard ../bash-tools)", "")
	BASH_TOOLS := ../bash-tools
else
	BASH_TOOLS := .
endif

ifndef TAG
	TAG := $(shell perl -n -e '/^\s*ARG\s+.+_VERSION\s*=\s*"?(\d+\.\d+|latest)"?/ && &{ print $$1; exit }' Dockerfile 2>/dev/null)
endif

ifndef REPO
	REPO :=
endif

# cassandra-dev     -> cassandra
# consul-dev        -> consul
# solrcloud-dev     -> solrcloud
# rabbitmq-cluster  -> rabbitmq
# solrloud          -> solr
# presto-cli        -> presto
BRANCH = $(shell echo "$(REPO)" | sed 's,.*/,,; s/-dev$$//; s/-cli$$//; s/-cluster$$//; s/cloud$$//')

ifndef MAP_PORTS
	MAP_PORTS := ""
endif

ifndef NOPORTS
	NOPORTS :=
endif

#ifndef RUN_ARGS
#	RUN_ARGS := bash
#endif

ifdef LATEST
	TAG=latest
endif

ifneq "$(TAG)" ""
	REPO_TAG := "$(REPO):$(TAG)"
else
	REPO_TAG := "$(REPO)"
endif

.PHONY: build
build:
	docker build -t $(REPO_TAG) .

.PHONY: cloudbuild
cloudbuild:
	gcloud builds submit --tag $(REPO_TAG) .

.PHONY: gcp-cloudbuild
gcp-cloudbuild:
	@# replace the prefix with the project ID
	gcloud builds submit --tag gcr.io/$$GOOGLE_CLOUD_PROJECT/`echo "$(REPO_TAG)" | sed 's,.*/,,'` .

.PHONY: gcp
gcp: gcp-cloudbuild
	@:

.PHONY: gcb
gcb: gcp-cloudbuild
	@:

.PHONY: azure-container-registry
azure-container-registry:
	az acr build --file Dockerfile --registry "$$AZURE_REGISTRY" --image "$(REPO_TAG)" .

.PHONY: azure
azure: azure-container-registry
	@:

.PHONY: acr
acr: azure
	@:

.PHONY: azure-container-registry-local
azure-container-registry-local:
	docker build -t "$$AZURE_REGISTRY.azurecr.io/`echo "$(REPO_TAG)" | sed 's,.*/,,'`" .

.PHONY: azure-local
azure-local: azure-container-registry-local
	@:

.PHONY: acr-local
acr-local: azure-local
	@:

.PHONY: acr2
acr2: acr-local
	@:

.PHONY: nocache
nocache:
	docker build -t $(REPO_TAG) --no-cache .

.PHONY: build-versions
build-versions:
	@echo "Building all versions from branches with base '$(BRANCH)'"
	for x in $$(git branch -a | grep $(BRANCH) | sed 's,remotes/origin/,,' | sort -u); do \
		git checkout "$$x" && \
		$(MAKE) build && \
		echo || \
		exit 1; \
	done; \
	git checkout master

.PHONY: build-push-versions
build-push-versions: build-versions push-versions
	:

.PHONY: push
push:
	docker push $(REPO_TAG)
	[ -f hooks/post_build ] && hooks/post_build || :

# use 'make push' rather than just 'docker push' to trigger hooks/post_build
.PHONY: push-versions
push-versions:
	for x in $$(git branch -a | grep $(BRANCH) | sed 's,remotes/origin/,,' | sort -u); do \
		git checkout "$$x" && \
		$(MAKE) push && \
		echo || \
		exit 1; \
	done; \
	git checkout master

.PHONY: up
up: build test push
	:

# ============================================================================ #
# complex fix to allow makefile testing to work
CMD := $(shell \
	if [ -n "$(MAP_PORTS)" ]; then \
		ports=$$(for port in $(MAP_PORTS); do echo "-p $$port:$$port "; done); \
		echo docker run --rm $$ports $(REPO_TAG); \
	elif [ -n "$(NOPORTS)" ]; then \
		echo docker run --rm $(REPO_TAG); \
	else \
		echo docker run -ti --rm $(REPO_TAG) $(RUN_ARGS); \
	fi \
)

CMD_FG := $(shell \
	if [ -n "$(MAP_PORTS)" ]; then \
		ports=$$(for port in $(MAP_PORTS); do echo "-p $$port:$$port "; done); \
		echo docker run -ti --rm $$ports $(REPO_TAG); \
	elif [ -n "$(NOPORTS)" ]; then \
		echo docker run --rm $(REPO_TAG); \
	else \
		echo "docker run --rm $(REPO_TAG)"; \
	fi \
)

.PHONY: runcmd
runcmd:
	echo "$(CMD)"

.PHONY: runcmd-fg
runcmd-fg:
	echo "$(CMD_FG)"

.PHONY: run
run: runcmd
	@#$(MAKE) run | tee /dev/stderr | sh
	@$(CMD)

.PHONY: run-fg
run-fg: runcmd-fg
	@$(CMD_FG)

# ============================================================================ #

.PHONY: sh
sh:
	#make -e MAP_PORTS="" run
	docker run -ti --rm -v $$PWD:/code $(REPO_TAG) sh

.PHONY: bash
bash:
	#make -e MAP_PORTS="" run
	docker run -ti --rm -v $$PWD:/code $(REPO_TAG) bash

.PHONY: test
test:
	#docker run -ti $(REPO) sh -c 'if test -f tests/all.sh; then tests/all.sh; fi;'
	docker run -ti $(REPO) tests/all.sh

.PHONY: interactive
interactive:
	docker run -ti --rm -v $$PWD:/code `awk '/^FROM /{print $$2; exit}' Dockerfile` sh

.PHONY: browse
browse:
	@source $(BASH_TOOLS)/.bash.d/network.sh; browser "https://github.com/harisekhon/dockerfiles"

.PHONY: github
github: browse
	@:

.PHONY: github-url
github-url:
	@source $(BASH_TOOLS)/.bash.d/functions.sh; echo "https://github.com/harisekhon/dockerfiles" | tee /dev/stderr | tr -d '\n' | paste_clipboard

.PHONY: dockerhub
dockerhub:
	@source $(BASH_TOOLS)/.bash.d/network.sh; browser "https://hub.docker.com/r/$(REPO)"

.PHONY: dockerhub-url
dockerhub-url:
	@source $(BASH_TOOLS)/.bash.d/functions.sh; echo "https://hub.docker.com/r/$(REPO)" | tee /dev/stderr | tr -d '\n' | paste_clipboard

.PHONY: dockerhub-status
dockerhub-status:
	check_dockerhub_repo_build_status.py -r $(REPO)
